﻿using Humanizer;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using WXWorkSvc.Common.Extensions;
using WXWorkSvc.Models;

namespace WXWorkSvc.Controllers;

[Route("api/[controller]")]
[ApiController]
public class ThirdMsgController : ControllerBase
{
    private readonly Cryptography _Cryptography;
    private readonly WXWork3rd _WXWork3rd;
    public ThirdMsgController(Cryptography cryptography, WXWork3rd wXWork)
    {
        _Cryptography = cryptography;
        _WXWork3rd = wXWork;
    }
    [HttpPost("text")]
    public async Task<IActionResult> TextAsync(string msg_signature, string timestamp, string nonce)
    {
        var sr = new StreamReader(Request.Body);
        var bd = await sr.ReadToEndAsync();
        if (!_Cryptography.VerifyURL<RequestThirdMessage>(msg_signature, timestamp, nonce, bd, out var req))
            return BadRequest();
        await _WXWork3rd.SendTextMessageAsync(req!.SuiteId, req.CorpId, req.UserIds, req.Message);
        return Ok();
    }
    [HttpPost("md")]
    public async Task<IActionResult> MDAsync(string msg_signature, string timestamp, string nonce)
    {
        var sr = new StreamReader(Request.Body);
        var bd = await sr.ReadToEndAsync();
        if (!_Cryptography.VerifyURL<RequestThirdMessage>(msg_signature, timestamp, nonce, bd, out var req))
            return BadRequest();
        await _WXWork3rd.SendMDMessageAsync(req!.SuiteId, req.CorpId, req.UserIds, req.Message);
        return Ok();
    }

}
